package com.zjmiec.service.impl;
import com.zjmiec.entity.pptInfo;
import com.zjmiec.entity.pptJob;
import com.zjmiec.mapper.pptInfoMapper;
import com.zjmiec.mapper.pptJobMapper;
import com.zjmiec.service.pptInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjmiec.utils.powerPointUtil;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import static com.zjmiec.utils.aliOSSUtil.getUrls;
import static com.zjmiec.utils.aliOSSUtil.uploadFile;
/**
 * 服务实现类
 * @author li
 * @since 2023-03-02
 */
@Service
public class pptInfoServiceImpl extends ServiceImpl<pptInfoMapper, pptInfo> implements pptInfoService {
    @Autowired
    private pptInfoMapper pptInfoMapper;
    @Autowired
    private pptJobMapper pptJobMapper;
    private static byte[] pictureData;
    @Override
    public void QueryInfo() throws IOException {
        //计数
        int i = 0;
        //ppt文件路径及文件名称
        String rootDir = "src/main/resources/ppt/Sample.pptx";
        //查询pptInfo表
        List<pptInfo> InfoList = pptInfoMapper.selectList(null);
        for (pptInfo pptInfoList : InfoList) {
            //参数map
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("${name}", pptInfoList.getName());
            paramMap.put("${introduce}", pptInfoList.getIntroduce());
            paramMap.put("${brand}", pptInfoList.getBrand());
            paramMap.put("${model}", pptInfoList.getModel());
            paramMap.put("${number}", pptInfoList.getNumber());
            paramMap.put("${color}", pptInfoList.getColor());
            paramMap.put("${weight}", String.valueOf(pptInfoList.getWeight()));
            paramMap.put("${area}", pptInfoList.getArea());
            paramMap.put("${mprice}", String.valueOf(pptInfoList.getMprice()));
            paramMap.put("${pprice}", String.valueOf(pptInfoList.getPprice()));
            paramMap.put("${location}", pptInfoList.getLocation());
            List<String> urls = getUrls(pptInfoList.getSpu_pic());
            i++;
            ArrayList<byte[]> list= new ArrayList<>();
            for (String urlList : urls) {
                URL imageUrl = new URL(urlList);
                InputStream inputStream = imageUrl.openStream();
                pictureData = IOUtils.toByteArray(inputStream);
                list.add(pictureData);
            }
            //ppt操作
            powerPointUtil.mergePPT(list, rootDir, paramMap);
            String url = uploadFile("Result.pptx", "malltest/Result" + i + ".pptx");
            //插入ppt_job表
            pptJob pptjob = new pptJob();
            pptjob.setUrl(url);
            pptjob.setIdFinish("0");
            pptjob.setIsDel("0");
            int insert = pptJobMapper.insert(pptjob);
        }
    }
}